package fr.migo.quisonne.android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class QuiSonnePreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener{

	public final static String PREF_START="pref_start";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		addPreferencesFromResource(R.xml.preferences);
		
		SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
		sharedPreferences.registerOnSharedPreferenceChangeListener(this);
		
		manageService(sharedPreferences);
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		
		if(PREF_START.equals(key)){
			manageService(sharedPreferences);
		}
	}
	
	public void manageService (SharedPreferences sharedPreferences){
		boolean started=sharedPreferences.getBoolean(PREF_START, true);
		if(started){
			startService(new Intent(this,QuiSonneService.class));
			Toast.makeText(this, R.string.service_start, Toast.LENGTH_SHORT).show();
		}
		else {
			stopService(new Intent(this,QuiSonneService.class));
			Toast.makeText(this, R.string.service_stop, Toast.LENGTH_SHORT).show();
		}
	}
}
